/**
 * @fileoverview
 * TODO doc me
 */


/**
 * @class
 * @constructor
 */
function Application() {
	
	var widget = null;
	
	var that = this;
	
	// called by this.init
	
	this.initWidget = function() {
		
		// initialise widget

		flyui.info("instantiate a service for the widget");
		var service = new flyui.flybasepub.Service();
		service.setEndpoint("../../../data/flybase");
		
		flyui.info("instantiate a renderer for the widget");
		var renderer = new flyui.flybasepub.PublicationWidget.DefaultRenderer(); 
		renderer.setCanvas($("#flybasepubPublicationWidget"));
		widget = new flyui.flybasepub.PublicationWidget(service, renderer);

	};
	

	this.init = function() {
		
		var logReader = new YAHOO.widget.LogReader("logger");

		this.initWidget();

		// attach form submission listener to form
		flyui.info("hook form submission to widget call");
		YAHOO.util.Event.addListener("queryForm", "submit", this.onFormSubmitted);
		
		flyui.info("application loaded, showing main pane");
		$("#loadingPane").hide();
		$("#applicationPane").show();
	};
	
	
	this.onFormSubmitted = function(event) {
	    try {
    		flyui.info("form submitted");
    		var query = document.getElementById("queryInput").value;
    		flyui.debug("query: "+query);
    		
    		$("queryInput").value = "";
    		widget.findPublicationsByFlybaseGeneID(query);
    		flyui.mvcutils.show(document.getElementById("flybasepubPublicationWidget"));
	    }
        catch (error) {
            flyui.err(error.name+" "+error.message);
        }
	};

	this.init();
}

// on dom ready, instantiate application
YAHOO.util.Event.onDOMReady(function() {new Application();});


//function initialiseApplication() {
//	
//	var logReader = new YAHOO.widget.LogReader("logger");
//	
//	log("instantiate a service for the flybase publication widget");
//	var service = new flyui.flybasepub.Service("../../../data/flybase");
//	
//	log("instantiate a renderer for the flybase publication widget");
//	var renderer = new flyui.flybasepub.PublicationWidget.DefaultRenderer(); 
//    renderer.setCanvas($("#flybasepubPublicationWidget"));
//    
//    flyui.info("instantiate a flybase publication widget");
//    flybasepub = new flyui.flybasepub.PublicationWidget(service, renderer);
//		
////	var renderPane = document.getElementById("flybasePublicationWidget");
////	var renderer = new flyui.flybasepub.PublicationWidget.DefaultRenderer();
////	renderer.setCanvas(renderPane);
////	
////	log("instantiate a flybase publication widget");
////	widget = new flyui.flybasepub.PublicationWidget(service, renderer);
////	
//	log("hook form submission to widget call");
//	YAHOO.util.Event.addListener("queryForm", "submit", onFormSubmit);
//	
//	log("application loaded, showing main pane");
//	flyui.mvcutils.hide(document.getElementById("loadingPane"));
//	flyui.mvcutils.show(document.getElementById("applicationPane"));
//}
//
//function onFormSubmit(event) {
//	log("form submitted");
//	
//	var query = document.getElementById("queryInput").value;
//	log("query: "+query);
//	
//	var fbgnMode = document.getElementById("fbgnInputMode").checked;
//	log("fbgn mode: "+fbgnMode);
//
//	if (fbgnMode) {
//		widget.findPublicationsByFlybaseGeneID(query);
//	} else {
//		//widget.findImagesByFlybaseGeneID(query);
//	}
//}
//
//YAHOO.util.Event.onDOMReady(initialiseApplication);